package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping
    public R<String> dish(@RequestBody DishDto dishDto) {
        dishService.saveAndFlavor(dishDto);
        //数据库同步缓存
        redisTemplate.delete("dishList_" + dishDto.getCategoryId());

        return R.success("success");
    }

    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {

        Page<DishDto> pages = dishService.page(page, pageSize, name);
        return R.success(pages);

    }


    @GetMapping("{id}")
    // TODO: 2022/2/26 0026
    //为什么Long换成Integer会报错
    public R<DishDto> getById(@PathVariable Long id) {
        //查询redis
        DishDto redisData = (DishDto) redisTemplate.opsForValue().get("dishList_" + id);
        if(redisData!=null){
            return R.success(redisData);
        }

        DishDto dishDto = dishService.getByIdAndFlavor(id);

        //初次查询放入缓存
        redisTemplate.opsForValue().set("dishList_" + id,dishDto);

        return R.success(dishDto);
    }

    @PutMapping
    //加上事务管理
    //@Transactional
    public R<String> update(@RequestBody DishDto dishDto) {

        dishService.updateAndFlavor(dishDto);

       // redisTemplate.delete("dishList_" + dishDto.getCategoryId());
        Set keys = redisTemplate.keys("dishList_*");
        redisTemplate.delete(keys);
        return R.success("修改成功");
    }

  /*  @GetMapping("list")
    //加上事务管理
    //@Transactional
    public R<List<Dish>> update( Long categoryId) {
        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class).eq(categoryId != null, Dish::getCategoryId, categoryId);
        List<Dish> list = dishService.list(queryWrapper);
        return R.success(list);
    }*/


    @GetMapping("list")
    //加上事务管理
    @Transactional
    //在查询菜单的上加上查询对应的口味,供客户端展示
    public R<List<DishDto>> update( Long categoryId) {
        //查询redis
        List<DishDto> redisData = (List<DishDto>) redisTemplate.opsForValue().get("dishList_" + categoryId);
        if(redisData!=null){
            return R.success(redisData);
        }

        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class).eq(categoryId != null, Dish::getCategoryId, categoryId);
        List<Dish> list = dishService.list(queryWrapper);
        List<DishDto> dishDtoList=new ArrayList<>();
        for (Dish dish : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);

            LambdaQueryWrapper<DishFlavor> wrapper = Wrappers.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId, dish.getId());

            List<DishFlavor> dishFlavors = dishFlavorService.list(wrapper);
            dishDto.setFlavors(dishFlavors);
            dishDtoList.add(dishDto);
        }

        //放入redis
        redisTemplate.opsForValue().set("dishList_"+categoryId,dishDtoList);

        return R.success(dishDtoList);
    }


    //根据id删除菜品
    @DeleteMapping
    public R<String> delete(Long ids) {

        dishService.removeByIds(Arrays.asList(ids));
        //删除菜品后删除缓存
        Set keys = redisTemplate.keys("dishList_*");
        redisTemplate.delete(keys);
        return R.success("删除菜品成功");
    }

}
